package data.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import po.ResultMessage;
import po.UserPO;
import po.UserStatus;
import service.CustomerDataService;
import service.UserDataService;

public class UserData extends UnicastRemoteObject implements UserDataService {
	private static final long serialVersionUID = 1L;
	private List<UserPO> salesManagers = new ArrayList<UserPO>();
	private List<UserPO> generalManagers = new ArrayList<UserPO>();
	private ObjectInputStream inputStream;
	private ObjectOutputStream outputStream;
	
	public UserData(CustomerDataService customerData) throws RemoteException{  
//		try {
//			initData();
//			openOutputStream();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		initTestData();
	}
	
	public void initTestData() {  
		salesManagers.add(new UserPO("a", "111111", "aaa", "aaa@salesManager.com", UserStatus.SALES_MANAGER));
		salesManagers.add(new UserPO("b", "111111", "bbb", "bbb@salesManager.com", UserStatus.SALES_MANAGER));
		salesManagers.add(new UserPO("c", "111111", "ccc", "ccc@salesManager.com", UserStatus.SALES_MANAGER));
		generalManagers.add(new UserPO("a", "111111", "aaa", "aaa@generalManager.com", UserStatus.GENERAL_MANAGER));
		generalManagers.add(new UserPO("b", "111111", "bbb", "bbb@generalManager.com", UserStatus.GENERAL_MANAGER));
		generalManagers.add(new UserPO("c", "111111", "ccc", "ccc@generalManager.com", UserStatus.GENERAL_MANAGER));
	}
	
	@SuppressWarnings("unchecked")
	private void initData() throws IOException {
		if (new File("data/user").exists()) {
			FileInputStream fis = new FileInputStream("data/user");
			inputStream = new ObjectInputStream(fis);

			while (fis.available() > 0) {
				try {
					salesManagers = (List<UserPO>) inputStream.readObject();
					generalManagers = (List<UserPO>) inputStream.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
			inputStream.close();
		}
	}

	private void openOutputStream() throws IOException {
		FileOutputStream fos = new FileOutputStream("data/user");
		outputStream = new ObjectOutputStream(fos);
		outputStream.writeObject(salesManagers);
		outputStream.writeObject(generalManagers);
	}

	public synchronized void save() throws IOException {
		close();
		openOutputStream();
	}

	public synchronized void close() {
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public ResultMessage logIn(UserPO user) throws RemoteException {
		List<UserPO> usersToBeSearched = user.getStatus() == UserStatus.SALES_MANAGER ? salesManagers : generalManagers;
		for(UserPO tmp : usersToBeSearched) {
			System.out.println(tmp.getAccount() + "  " + tmp.getPassword());
			if(tmp.getAccount().equals(user.getAccount()) && tmp.getPassword().equals(user.getPassword()))
				return ResultMessage.SUCCEED;
		}
		return ResultMessage.REJECTED;
	}
	
	@Override
	public ResultMessage add(UserPO user) throws RemoteException{
		switch (user.getStatus()) {
		case SALES_MANAGER:
			salesManagers.add(user);
			return ResultMessage.SUCCEED;
		case GENERAL_MANAGER:
			generalManagers.add(user);
			return ResultMessage.SUCCEED;
		default:
			return ResultMessage.ERROR;
		}
	}

	@Override
	public ResultMessage remove(UserPO user) throws RemoteException{ 
		switch (user.getStatus()) {
		case SALES_MANAGER:
			salesManagers.remove(user);
			return ResultMessage.SUCCEED;
		case GENERAL_MANAGER:
			generalManagers.remove(user);
			return ResultMessage.SUCCEED;
		default:
			return ResultMessage.ERROR;
		}
	}

	@Override
	public ResultMessage update(UserPO user) throws RemoteException{
		List<UserPO> users;
		if(user.getStatus() == UserStatus.SALES_MANAGER)
			users = salesManagers;
		else 
			users = generalManagers;
		for (int i = 0; i < users.size(); i++) {
			if(users.get(i).getAccount().equals(user.getAccount())){
				users.set(i, user);
				return ResultMessage.SUCCEED;
			}
		}
		return ResultMessage.ERROR;
	}

	@Override
	public List<UserPO> getAll(UserStatus status) throws RemoteException{
		switch (status) {
		case SALES_MANAGER:
			return salesManagers;
		case GENERAL_MANAGER:
			return generalManagers;
		default:
			return new LinkedList<UserPO>();
		}
	}
}